using Org.Apache.Rocketmq;
using Vin.Tool.Attributes;
using Vin.Tool.Core.AssertCategory;

namespace Vin.Extension.RocketMQ.Options;

[Option("VinRocketMq")]
public class RocketMqOption
{
    /// <summary>
    /// AccessKey
    /// </summary>
    public string? AccessKey { get; set; }

    /// <summary>
    /// SecretKey
    /// </summary>
    public string? SecretKey { get; set; }

    /// <summary>
    /// 服务器地址
    /// </summary>
    public string? Endpoint { get; set; }

    /// <summary>
    /// 命名空间
    /// </summary>
    public string? Namespace { get; set; }

    /// <summary>
    /// 请求超时时间
    /// </summary>
    public int? RequestTimeoutSecond { get; set; }

    public ClientConfig ClientConfig => CreateClientConfig();

    private ClientConfig CreateClientConfig()
    {
        if (string.IsNullOrWhiteSpace(Endpoint))
        {
            throw new Exception("Endpoint 不能为空");
        }

        var clientConfig = new ClientConfig.Builder()
            .SetEndpoints(Endpoint);
        if (AccessKey.IsNotNullOrWhiteSpace() && SecretKey.IsNotNullOrWhiteSpace())
        {
            clientConfig.SetCredentialsProvider(new StaticSessionCredentialsProvider(AccessKey, SecretKey));
        }

        if (Namespace.IsNotNullOrWhiteSpace()) clientConfig.SetNamespace(Namespace);

        if (RequestTimeoutSecond.HasValue)
            clientConfig.SetRequestTimeout(TimeSpan.FromSeconds(RequestTimeoutSecond.Value));

        return clientConfig.Build();
    }
}